//
//  LQStatusOriginView.swift
//  Weibo
//
//  Created by ios on 16/8/12.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class LQStatusOriginView: UIView {
    //  记录原创微博底部约束
    var originViewConstraint: Constraint?
    //加载数据
    var  statusViewModel:LQStatusViewModel?{
        didSet{
            //设置头像数据
            if let profile = statusViewModel?.status?.user?.profile_image_url{
                iconImg.sd_setImageWithURL(NSURL(string: profile), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //设置昵称
             screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            //设置内容
            contenLabel.text = statusViewModel?.status?.text
            //来源
            sourceLabel.text = statusViewModel?.sourceContent
           //会员等级
            mbrankImageView.image = statusViewModel?.mbrankImg
           //认证类型等级图片
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImg
            //发微博的时间
            timeLabel.text = statusViewModel?.timeCount
            //  卸载上次约束
            originViewConstraint?.uninstall()
            //  判断原创微博的配图个数是否大于0
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0 {
                //  有配图并且个数大于0
                //  显示配图
                pictureView.hidden = false
                //  绑定原创微博数据源
                pictureView.picUrls = picUrls
                
                //  更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originViewConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
                
                
            } else {
                //  隐藏配图
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originViewConstraint = make.bottom.equalTo(contenLabel).offset(StatusTableViewCellMargin).constraint
                })
            }

        
        }
    }
    
 //MARK: --懒加载控件
    //头像
    private lazy var iconImg:UIImageView = UIImageView(image:  UIImage(named: "avatar_default_big"))
    //认证类型等级图片
    private lazy var verifiedTypeImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //昵称
    private lazy var screenNameLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    //会员等级
    private lazy var mbrankImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(12)
        label.textColor = UIColor.orangeColor()
        return label
    }()
    //来源
    private lazy var sourceLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(12)
        label.textColor = UIColor.lightGrayColor()
        return label
    }()
    //微博内容
    private lazy var contenLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        return label
    }()
    //配图
    private lazy var pictureView:LQStatusPictureView = {
      let view = LQStatusPictureView()
       view.backgroundColor = self.backgroundColor
      return view
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
     //  添加控件设置约束
    private func setupUI(){
    backgroundColor = UIColor.whiteColor()
    //添加控件
        addSubview(iconImg)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contenLabel)
        addSubview(pictureView)
        
     //设置约束
        iconImg.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImg.snp_trailing)
            make.centerY.equalTo(iconImg.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconImg.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(iconImg)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(iconImg)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImg)
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusTableViewCellMargin)
        }
        
        contenLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconImg)
            make.top.equalTo(iconImg.snp_bottom).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contenLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(contenLabel)
        }
        //  关键约束  原创微博视图的底部约束 = 微博内容的底部约束 + 间距
        
        self.snp_makeConstraints { (make) -> Void in
            self.originViewConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
        
    }

    
}
